﻿using PRO.User;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using PRO.Common;
using PRO.Product;
using System.Data;
using PRO.Entity;
using PRO.Config;

namespace web.deal
{
    public partial class checkout : PageBase
    {
        protected int Products = 0;
        protected int Addr_Counts = 0;
        protected decimal TotalPrice = 0;
        protected string GUID = Guid.NewGuid().ToString();
        protected void Page_Load(object sender, EventArgs e)
        {
            this.Title = "结算-" + BaseConfigs.Title;
            Session["GUID"] = GUID;
            if (UserInfo.ID == 0)
                Response.Redirect("/login?url=" + Request.RawUrl.UrlEncode());
            else
            {
                if(!IsPostBack)
                    Bind(); 
            }
        }
        private void Bind()
        {
            DataTable dt = ShopCart.GetCartItems(out TotalPrice);
            if (dt != null && dt.Rows.Count > 0)
            {
                Products = dt.Rows.Count;
                List<Deliver_address_Info> list = DeliverLib.GetMyDeliver_Address(UserInfo.ID);
                if (list != null && list.Count > 0)
                {
                    Addr_Counts = list.Count;
                    Deliver_address_Info default_addr = list.Find(delegate(Deliver_address_Info p) { return p.Default_Addr == true; });
                    if (default_addr == null)
                        list[0].Default_Addr = true;
                    this.Repeater1.DataSource = list;
                    this.Repeater1.DataBind();
                }


                this.Repeater2.DataSource = dt;
                this.Repeater2.DataBind();
            }
            else
                this.ErrorControl.Msg = "没有可购买的商品！";
        }
    }
}